package com.yxq.springboot.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.yxq.springboot.bean.SpringBootUser;
import com.yxq.springboot.dao.SpringBootUserDao;

/**
 * @author yixiaoqun
 */
@Repository
public class SpringBootUserDaoImpl implements SpringBootUserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public int insert(SpringBootUser springBootUser) {
		String sql = "insert into springboot_user (name, create_time) values (?, ?)";
		return jdbcTemplate.update(sql, springBootUser.getName(), springBootUser.getCreateTime());
	}

	@Override
	public int deleteById(int id) {
		String sql = "delete from springboot_user where id=?";
		return jdbcTemplate.update(sql, id);
	}

	@Override
	public int updateById(SpringBootUser springBootUser) {
		String sql = "update springboot_user set name=?, create_time=? where id=?";
		return jdbcTemplate.update(sql, springBootUser.getName(), springBootUser.getCreateTime(), springBootUser.getId());
	}

	@Override
	public SpringBootUser selectById(int id) {
		String sql = "select * from springboot_user where id=?";
		return jdbcTemplate.queryForObject(sql, new RowMapper<SpringBootUser>() {
			@Override
			public SpringBootUser mapRow(ResultSet rs, int rowNum) throws SQLException {
				SpringBootUser springBootUser = new SpringBootUser();
				springBootUser.setId(rs.getInt("id"));
				springBootUser.setName(rs.getString("name"));
				springBootUser.setCreateTime(rs.getDate("create_time"));
				return springBootUser;
			}
		}, id);
	}

}
